package crazy.crack;

public class DuplicateCharacters
{
	public static void main(String[] args)
	{
		String string = "abcd";
		char[] str = string.toCharArray();
		removeDuplicatesEff(str);
	}
	
	public static void removeDuplicates(char[] str)
	{
		if (str == null)
			return;
		int len = str.length;
		if (len < 2)
			return;

		int tail = 1;

		for (int i = 1; i < len; ++i)
		{
			int j;
			for (j = 0; j < tail; ++j)
			{
				if (str[i] == str[j])
					break;
			}
			if (j == tail)
			{
				str[tail] = str[i];
				++tail;
			}
		}
		str[tail] = 0;
	}
	
	public static void removeDuplicatesEff(char[] str)
	{
		if (str ==  null) return;
		int len = str.length;
		if (len < 2) return;
		
		boolean[] hit = new boolean[256];
		for (boolean b : hit)
		{
			System.out.println(b);
		}
		hit[str[0]] = true;
		int tail = 1;
		for (int i = 1; i < len; ++i)
		{
			if (!hit[str[i]])
			{
				str[tail] = str[i];
				++tail;
				hit[str[i]] = true;
			}
		}
		str[tail] = 0;
	}
}
